package com.wuye.basic.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wuye.basic.domain.*;
import com.wuye.basic.domain.dto.WyBalconyDTO;
import com.wuye.basic.service.*;
import com.wuye.basic.service.impl.WyHomeServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wuye.common.annotation.Log;
import com.wuye.common.core.controller.BaseController;
import com.wuye.common.core.domain.AjaxResult;
import com.wuye.common.enums.BusinessType;
import com.wuye.common.utils.poi.ExcelUtil;
import com.wuye.common.core.page.TableDataInfo;

/**
 * 业主Controller
 * 
 * @author avalon
 * @date 2024-11-25
 */
@RestController
@RequestMapping("/basic/owner")
public class WyOwnerController extends BaseController
{
    @Autowired
    private IWyOwnerService wyOwnerService;
    @Autowired
    private IWyHomeService wyHomeService;

    @Autowired
    private IWyUnitService wyUnitService;
    @Autowired
    private IWyBalconyService wyBalconyService;
    @Autowired
    private WyBalconyunithomeService wyBalconyunithomeService;

    /**
     * 查询业主列表
     */
    @PreAuthorize("@ss.hasPermi('basic:owner:list')")
    @GetMapping("/list")
    public TableDataInfo list(WyOwner wyOwner)
    {

        startPage();
        List<WyOwner> list = wyOwnerService.selectWyOwnerList(wyOwner);
        for (WyOwner item : list) {
            QueryWrapper<WyHome> qw = new QueryWrapper<>();
        if (item.getOwnerId() != null){
            qw.eq("ownerId", item.getOwnerId());
        }
            List<WyHome> list1 = wyHomeService.list(qw);
            Long size =(long) list1.size();
           item.setNum(size);
//            if (one==null){
//            continue;
//        }
//            item.setWyHome(one);
//            QueryWrapper<WyBalconyunithome> qw1=new QueryWrapper<>();
//        if (one.getHomeId() != null){
//            qw1.eq("homeId", one.getHomeId());
//        }
//        WyBalconyunithome one1 = wyBalconyunithomeService.getOne(qw1);
//            WyUnit wyUnit = wyUnitService.selectWyUnitByUnitId(one1.getUnitid());
//        WyBalcony wyBalcony = wyBalconyService.selectBybalconyId(one1.getBalconyid());
//        item.setWyHome(one);
//        item.setWyUnit(wyUnit);
//        item.setWyBalcony(wyBalcony);
        };
        return getDataTable(list);
    }

    /**
     * 导出业主列表
     */
    @PreAuthorize("@ss.hasPermi('basic:owner:export')")
    @Log(title = "业主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WyOwner wyOwner)
    {
        List<WyOwner> list = wyOwnerService.selectWyOwnerList(wyOwner);
        ExcelUtil<WyOwner> util = new ExcelUtil<WyOwner>(WyOwner.class);
        util.exportExcel(response, list, "业主数据");
    }

    /**
     * 获取业主详细信息
     */
    @PreAuthorize("@ss.hasPermi('basic:owner:query')")
    @GetMapping(value = "/{ownerId}")
    public AjaxResult getInfo(@PathVariable("ownerId") Long ownerId)
    {
        return success(wyOwnerService.selectWyOwnerByOwnerId(ownerId));
    }

    /**
     * 新增业主
     */
    @PreAuthorize("@ss.hasPermi('basic:owner:add')")
    @Log(title = "业主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WyOwner wyOwner)
    {
        return toAjax(wyOwnerService.insertWyOwner(wyOwner));
    }

    /**
     * 修改业主
     */
    @PreAuthorize("@ss.hasPermi('basic:owner:edit')")
    @Log(title = "业主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WyOwner wyOwner)
    {
        return toAjax(wyOwnerService.updateWyOwner(wyOwner));
    }

    /**
     * 删除业主
     */
    @PreAuthorize("@ss.hasPermi('basic:owner:remove')")
    @Log(title = "业主", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ownerIds}")
    public AjaxResult remove(@PathVariable Long[] ownerIds)
    {
        return toAjax(wyOwnerService.deleteWyOwnerByOwnerIds(ownerIds));
    }
}
